package com.amos.think.service.mq;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageService {
    private final RocketMQProducer rocketMQProducer;
    private final RocketMQConsumer rocketMQConsumer;

    @Autowired
    private ObjectMapper objectMapper;

    public MessageService(RocketMQProducer rocketMQProducer, RocketMQConsumer rocketMQConsumer) {
        this.rocketMQProducer = rocketMQProducer;
        this.rocketMQConsumer = rocketMQConsumer;
    }

    public void sendMessage(String topic, String tags, String message) throws Exception {
        rocketMQProducer.send(topic, tags, message);
    }
    public void sendJsonMessage(String topic, Object payload) throws Exception {
        String jsonString = objectMapper.writeValueAsString(payload);
        rocketMQProducer.send(topic,"createUser", jsonString);
    }

    public void startConsumer() throws Exception {
        rocketMQConsumer.start();
    }

    public void shutdownProducer() {
        rocketMQProducer.shutdown();
    }

    public void shutdownConsumer() {
        rocketMQConsumer.shutdown();
    }
}
